import { 
  AmbientLight,
  AnimationAction,
  AnimationMixer,
  BoxGeometry,
  Clock,
  Color,
  IcosahedronGeometry,
  Mesh,
  MeshLambertMaterial,
  MeshStandardMaterial,
  PerspectiveCamera,
  PlaneGeometry,
  PointLight,
  Scene, 
  SphereGeometry, 
  SpotLight, 
  SpotLightHelper, 
  TextureLoader, 
  WebGLRenderer
} from "three";
import stats from '../utils/stats';
// 调用
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls';
import { GUI } from 'dat.gui';

let scene = new Scene();
let renderer = setRenderer();
let camera = setCamera();

setLight();
init();

const trackballControls = new OrbitControls(camera, renderer.domElement);
// 调整场景位置
trackballControls.target.set(0, 3, 0);


function render() {
  stats.update();
  trackballControls.update();

  requestAnimationFrame(render);
  renderer.render(scene, camera);
}
render();

function setRenderer() {
  const renderer = new WebGLRenderer({ antialias: true });
  renderer.setPixelRatio(window.devicePixelRatio);
  renderer.setSize(window.innerWidth, window.innerHeight);
  // 支持阴影生成 
  renderer.shadowMap.enabled = true;
  document.body.appendChild(renderer.domElement);

  return renderer
}

function setCamera(): PerspectiveCamera {
  let camera = new PerspectiveCamera(45, window.innerWidth/window.innerHeight, 0.1, 1000);
  camera.position.set(0, 20, 40);
  scene.add( camera );

  return camera;
}

// 灯光设置
function setLight() {
  scene.add(new AmbientLight(0x444444));
  scene.add(new AmbientLight(0x343434));
  
  let spotLight = new SpotLight(0xffffff);
  spotLight.position.set(-10, 30, 40);
  spotLight.shadow.mapSize.width = 2048;
  spotLight.shadow.mapSize.height = 2048;
  spotLight.shadow.camera.fov = 15;
  spotLight.decay =2;
  spotLight.penumbra = 0.05;
  spotLight.castShadow = true;

  scene.add(spotLight);
  // scene.add(new SpotLightHelper(spotLight));
}

/** 功能区 */
function init() {
  let textureLoader = new TextureLoader();

  let planeGeom = new PlaneGeometry(60, 40, 1, 1);
  let planeMate = new MeshLambertMaterial({
    color: 0xffffff,
    map: textureLoader.load('/textures/general/floor-wood.jpg')
  });
  const plane = new Mesh(planeGeom, planeMate);
  plane.receiveShadow = true;
  plane.rotation.x = -Math.PI / 2;
  scene.add(plane);

  // 注意 widthSegments、heightSegments 不可太低，否则无效果
  let sphereGeom = new SphereGeometry(8, 180, 180);
  let sphereMate = new MeshStandardMaterial({
    map: textureLoader.load('/textures/w_c.jpg'),
    displacementMap: textureLoader.load('/textures/w_d.png'),
    metalness: 0.02,
    roughness: 0.07,
    color: 0xffffff
  });
  let mesh = new Mesh(sphereGeom, sphereMate);
  mesh.position.y = 9;
  mesh.castShadow = true;
  scene.add(mesh);

  addGUI(sphereMate);
}


function addGUI(mesh: MeshStandardMaterial): void {
  const controls = {
    displacementScale: 0,
    displacementBias: 0,
  }
  const gui = new GUI();  
  gui.add(controls, 'displacementScale', -5, 5, 0.01).onChange(val => {
    mesh.displacementScale = val
  });
  gui.add(controls, 'displacementBias', -5, 5, 0.01).onChange(val => {
    mesh.displacementBias = val
  })
}